import re

from fastapi import APIRouter
from re_common.baselibrary.tools.all_requests.aiohttp_request import AioHttpRequest
from re_common.baselibrary.tools.all_requests.mrequest import MRequest
from re_common.baselibrary.utils.core.mlamada import bools_string

from apps.allsubdb.paper_kejiribao.models import PaperKeJiRiBaoGetDay, headers, PaperKeJiRiBaoDown
from apps.core.m_route import ContextIncludedRoute
from apps.core.return_info import InputInfoModel, ReturnInfo, FAILED

router = APIRouter(route_class=ContextIncludedRoute)


@router.post("/step1/get_day")
async def get_page_link(input: InputInfoModel[PaperKeJiRiBaoGetDay]):
    """
       根据报纸的月份返回该报纸所有的可下载日期 marks:datelist
       :param proxy: 代理
       :param periodMonth:报纸月份:202008
       :return:
    """

    proxy = input.proxy
    periodMonth = input.data.periodMonth
    # years = periodMonth[0:4]
    # months = periodMonth[-2:]
    marks = input.data.marks
    return_info = ReturnInfo()
    url = r"http://digitalpaper.stdaily.com/http_www.kjrb.com/kjrb/html/{}-{}/period.xml".format(periodMonth[0:4], periodMonth[4:6])
    # this_header = headers.copy()
    rrq = AioHttpRequest()
    rrq.set_url(url) \
        .set_timeout(30).set_marks(marks) \
        .set_proxy(proxy) \
        .set_middler_list(
        [rrq.status_code_middlerwares, rrq.marks_middlerwares])
    bools, dicts = await rrq.run(MRequest.GET)
    return_info.status = bools_string(bools)
    return_info.msg_code = dicts["code"]
    return_info.msg = dicts["msg"]
    return_info.data = {"html": rrq.html}
    return return_info.todict()


@router.post("/step2/get_page_link")
async def get_page_link(input: InputInfoModel[PaperKeJiRiBaoDown]):
    """
       根据报纸的首版返回该报纸所有的版面标题和链接 marks:titleList1
       :param proxy: 代理
       :param periodDate:报纸日期:20200805
       :return:
    """
    proxy = input.proxy
    periodDate = input.data.periodDate
    marks = input.data.marks
    return_info = ReturnInfo()
    url = input.data.url
    this_header = headers.copy()
    this_header["host"] = "digitalpaper.stdaily.com"
    rrq = AioHttpRequest()
    rrq.set_url(url) \
        .set_timeout(30).set_marks(marks) \
        .set_proxy(proxy).set_header(this_header) \
        .set_middler_list(
        [rrq.status_code_middlerwares, rrq.marks_middlerwares])
    bools, dicts = await rrq.run(MRequest.GET)
    return_info.status = bools_string(bools)
    return_info.msg_code = dicts["code"]
    return_info.msg = dicts["msg"]
    return_info.data = {"html": rrq.html}
    return return_info.todict()

@router.post("/step3/get_article_link")
async def get_article_link(input: InputInfoModel[PaperKeJiRiBaoDown]):
    """
       根据报纸的版面url返回文章标题和链接 marks:titleList1
       :param proxy: 代理
       :param periodDate:报纸日期:20200805
       :return:
    """

    proxy = input.proxy
    periodDate = input.data.periodDate
    marks = input.data.marks
    return_info = ReturnInfo()
    url = input.data.url
    this_header = headers.copy()
    this_header["host"] = "digitalpaper.stdaily.com"
    rrq = AioHttpRequest()
    rrq.set_url(url) \
        .set_timeout(30).set_marks(marks) \
        .set_proxy(proxy).set_header(this_header).set_resp_errors("ignore") \
        .set_middler_list(
        [rrq.status_code_middlerwares, rrq.marks_middlerwares])
    bools, dicts = await rrq.run(MRequest.GET)
    return_info.status = bools_string(bools)
    return_info.msg_code = dicts["code"]
    return_info.msg = dicts["msg"]
    return_info.data = {"html": rrq.html}
    return return_info.todict()


#
#
@router.post("/step4/article_detail")
async def article_detail(input: InputInfoModel[PaperKeJiRiBaoDown]):
    """
       根据文章链接返回文章详情html marks: zoom
       :param proxy: 代理
       :param periodDate: 报纸日期
       :param title: 文章标题
       :param url: 文章链接 例:http://szb.farmer.com.cn/2020/20201202/20201202_002/20201202_002_1.htm

       :return:
    """
    return_info = ReturnInfo()
    proxy = input.proxy
    this_header = headers.copy()
    this_header["host"] = "digitalpaper.stdaily.com"
    url = input.data.url
    marks = input.data.marks
    rrq = AioHttpRequest()
    rrq.set_url(url) \
        .set_timeout(30).set_marks(marks) \
        .set_proxy(proxy).set_header(this_header).set_resp_errors("ignore") \
        .set_middler_list(
        [rrq.status_code_middlerwares, rrq.marks_middlerwares])
    bools, dicts = await rrq.run(MRequest.GET)
    return_info.status = bools_string(bools)
    return_info.msg_code = dicts["code"]
    return_info.msg = dicts["msg"]
    return_info.data = {"html": rrq.html}
    return return_info.todict()
